#include "queue.h"

queue *queue_create()
{
     queue *q = malloc(sizeof(queue));
     if (q == NULL)
          return NULL;
     q->head = q->tail = 0;
     return q;
}
int queue_isempty(queue *q)
{
     return (q->head == q->tail);
}

void queue_destory(queue **q)
{
     if (*q == NULL)
          return;
     free(*q);
     *q = NULL;
}

int queue_enqueue(queue *q, datatype *data)
{
     if ((q->tail + 1) % MAXSIZE == q->head)
          return -1;
     q->tail = (q->tail + 1) % MAXSIZE;
     q->data[q->tail] = *data;
}

int queue_dequeue(queue *q, datatype *data)
{
     if (queue_isempty(q))
          return -1;
     q->head = (q->head + 1) % MAXSIZE;
     *data = q->data[q->head];
     return 0;
}

void queue_show(queue *q)
{
     if (q->head == q->tail)
          return;
     int i = (q->head + 1) % MAXSIZE;
     while (i != q->tail)
     {
          printf("%d ", q->data[i]);
          i = (i + 1) % MAXSIZE;
     }
     printf("%d\n", q->data[i]);
}

void queue_clear(queue *q)
{
     q->head = q->tail;  //因为是循环的，所以这里直接让head和tail相等
}